﻿(function($) {
    $.fn.imageCrop = function (options) {
        var defaultOpt = {
            urlOrFile:null,   //文件url或者 file 对象
            cropSize: null,  //{width:"500px",height:"500px"}
            uploadUrl: "",
            fieldName: "",
            onSuccessCallback: null,
            onFailCallback: null,
            onPreview: null,  //上传前/上传后的预览回调
            fileField: null,  //如果需要指定特定的input file,,则在此设置,但,如果是需要强制裁剪的话,不会改变该file字段的数据内容,请在onSuccessCallback中读取
            sender: $(this)
        }

        var opt = $.extend({}, defaultOpt, options);

        var croppie = null;
        var img = new Image();

        var afterImageLoad = function () {
            var rate = opt.cropSize.width / opt.cropSize.height;

            if (img.naturalWidth/img.naturalHeight == rate) { //如果比例一致,则不切图
                if (opt.onSuccessCallback) {
                    __callBackFuncOrStr(opt.onSuccessCallback,
                        opt.sender,
                        [opt.urlOrFile]);
                }

                //layer.close(index);
            }


            var imageSize = __calcImageSize(
                img.naturalWidth,
                img.naturalHeight,
                 $(window).width() * 0.4,
                 $(window).height() * 0.4
                );

            //img.width = imageSize.width ;
            //img.height = imageSize.height;

            $(img).css("width", "100%");

            var div = $("<div class=\"croptest\"></div>");
            div.append(img);
            div.css("width", imageSize.width + "px");
            div.css("height", imageSize.height + "px");

            $("body").append(div);

            layer.open({
                type:1,
                content: div,
                maxWidth: $(window).width() * 0.7,
                maxHeight: $(window).height() * 0.7,
                //area: [imageSize.width + "px", (imageSize.height+56) + "px"],
                btn: ["确定", "取消"],
                success: function () {
                    //div.append(img);
                    croppie = new Cropper(img,
                        {
                            aspectRatio: opt.cropSize.width / opt.cropSize.height,
                            modal: false,
                            //cropBoxResizable: false,
                            viewMode:0,
                            //zoomOnTouch:false
           
                        });

                    croppie.crop();

                },
                end: function() {
                    croppie.destroy();
                    div.remove();
                },
                yes: function (index, layero) {
                    var result = croppie.getCroppedCanvas();
                    
                    result.toBlob(function (blob) {
                        var file = $(opt.urlOrFile);

                        if (file.is("input[type=file]")) {
                            file = file[0].files[0];
                        } else {
                            file = file[0];
                        }

                        blob.name = file.name;
                            if (opt.onSuccessCallback) {
                                __callBackFuncOrStr(opt.onSuccessCallback,
                                    opt.sender,
                                    [blob]);
                            }

                        layer.close(index);

                        
                        },
                        "image/jpeg",1);

                    //croppie.result(
                    //    {
                    //        type: 'blob',
                    //        format: 'jpeg',
                    //        size: opt.cropSize
                    //    }).then(function (data) {
                    //        //if (typeof (data) != typeof (Blob)) {
                    //        //    data = new Blob(data, { type: "image/jpg" });
                    //        //}

                           

                    //});

                    if (opt.onPreview) {
                        var base64 = result.toDataURL("image/jpeg", 0.9);
                        __callBackFuncOrStr(opt.onPreview, opt.sender, [base64]);
                    }
                    
                },
                btn2: function(index, layero) {
                    layer.close(index);
                }
            });
        }

        
        img.onload = afterImageLoad;
        

        if (!opt.urlOrFile && typeof (opt.urlOrFile) == typeof (String)) {
            $(img).attr("src", opt.urlOrFile);
        }
        else {
            

            __onReadFile(opt.urlOrFile,
                opt.sender,
                function (evt) {
                    $(img).attr("src", evt.target.result);
                });

        }
    }

})(jQuery);